﻿using System;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class HeaderFooterRecord : BiffRecord
    {
        private FrtHeader frtHeader = FrtHeader.Create(2204, FrtFlags.fFrtRef);
        private Guid guidSView;
        private ushort ABCD = 13116;
        private XLUnicodeString strHeaderEven;
        private XLUnicodeString strFooterEven;
        private XLUnicodeString strHeaderFirst;
        private XLUnicodeString strFooterFirst;
        private ushort cchHeaderEven;
        private ushort cchFooterEven;
        private ushort cchHeaderFirst;
        private ushort cchFooterFirst;

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            frtHeader.Read(context.Reader);
            guidSView = new Guid(context.ReadBytes(16));
            ABCD = context.ReadUInt16();

            var cchHeaderEven = context.ReadUInt16();
            var cchFooterEven = context.ReadUInt16();
            var cchHeaderFirst = context.ReadUInt16();
            var cchFooterFirst = context.ReadUInt16();
            strHeaderEven.Read(context.Reader, cchHeaderEven);
            strFooterEven.Read(context.Reader, cchFooterEven);
            strHeaderFirst.Read(context.Reader, cchHeaderFirst);
            strFooterFirst.Read(context.Reader, cchFooterFirst);
          //\\  throw new NotImplementedException();
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.HeaderFooter; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            frtHeader.Write(context);
            context.Write(guidSView.ToByteArray());
            context.Write(ABCD);
            strHeaderEven.WriteCharCount(context.Writer);
            strFooterEven.WriteCharCount(context.Writer);
            strHeaderFirst.WriteCharCount(context.Writer);
            strFooterFirst.WriteCharCount(context.Writer);
            strHeaderEven.WriteNoLength(context.Writer);
            strFooterEven.WriteNoLength(context.Writer);
            strHeaderFirst.WriteNoLength(context.Writer);
            strFooterFirst.WriteNoLength(context.Writer);
        }
    }
}